package com.yk.faceapplication.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.yk.faceapplication.MyApplication;
import com.yk.faceapplication.http.RequestUrl;


public class ImageUtil {

	public static String getRealFilePath( final Context context, final Uri uri ) {
		if ( null == uri ) return null;
		final String scheme = uri.getScheme();
		String data = null;
		if ( scheme == null )
			data = uri.getPath();
		else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
			data = uri.getPath();
		} else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
			Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
			if ( null != cursor ) {
				if ( cursor.moveToFirst() ) {
					int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
					if ( index > -1 ) {
						data = cursor.getString( index );
					}
				}
				cursor.close();
			}
		}
		return data;
	}

	public static void displayImage(Context cxt, String imageUrl, ImageView mImageView) {
		MyApplication mApplication = ((MyApplication) cxt.getApplicationContext());
		DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(false) // 设置下载的图片是否缓存在内存中
				.cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565).build();
		ImageLoader.getInstance().displayImage(RequestUrl.DOMAN_FILE+"get?token="+mApplication.getToken()+"&fileId="+imageUrl, mImageView, options);
	}
	public static void displayImage(Context cxt, Uri uri, ImageView mImageView) {
		MyApplication mApplication = ((MyApplication) cxt.getApplicationContext());
		DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(false) // 设置下载的图片是否缓存在内存中
				.cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565).build();
		ImageLoader.getInstance().displayImage(getRealFilePath(cxt,uri), mImageView, options);
	}

	public static void displayImage(Context cxt, String imageUrl, ImageView mImageView,String token) {
		MyApplication mApplication = ((MyApplication) cxt.getApplicationContext());
		DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(false) // 设置下载的图片是否缓存在内存中
				.cacheOnDisk(true)
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565).build();
		ImageLoader.getInstance().displayImage(RequestUrl.DOMAN_FILE+"get?token="+token+"&fileId="+imageUrl, mImageView, options);
	}
	public static void displayImage(Context cxt, String imageUrl, ImageView mImageView,String token,String xedFileGateUrl) {
		MyApplication mApplication = ((MyApplication) cxt.getApplicationContext());
		DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(false) // 设置下载的图片是否缓存在内存中
				.cacheOnDisk(true)
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565).build();
		ImageLoader.getInstance().displayImage(xedFileGateUrl+"?token="+token+"&fileId="+imageUrl, mImageView, options);
	}


	
	public static void displayImageWithListener(Context cxt, String imageUrl, ImageView mImageView,ImageLoadingListener listener) {
		DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(false) // 设置下载的图片是否缓存在内存中
				.cacheOnDisk(false) // 设置下载的图片是否缓存在SD卡中
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565).build();
		ImageLoader.getInstance().displayImage(imageUrl, mImageView, options,listener);
	}

}
